
#ifndef _APP_LOOP_H
#define _APP_LOOP_H

#ifdef __cplusplus
extern "C" {
#endif


//EPC模式发码相关
#define LOOPMODE_EPC_LOW_START                               500    //EPC模式起始位低电平
#define LOOPMODE_EPC_HIGHT_START                             2500 //EPC模式起始位高电平

#define LOOPMODE_EPC_LOW_1                                   300    //EPC模式1低电平
#define LOOPMODE_EPC_HIGHT_1                                 1750 //EPC模式1高电平

#define LOOPMODE_EPC_LOW_0                                   300    //EPC模式0低电平
#define LOOPMODE_EPC_HIGHT_0                                 1250 //EPC模式0高电平

#define LOOPMODE_EPC_LOW_SPACE                               300    //EPC模式空格位低电平
#define LOOPMODE_EPC_HIGHT_SPACE                             2500 //EPC模式空格位高电平


//9000模式发码相关
#define LOOPMODE_9000_LOW_START                              400    //9000模式起始位低电平
#define LOOPMODE_9000_HIGHT_START                            400 //9000模式起始位高电平

#define LOOPMODE_9000_LOW_1                                  200    //9000模式1低电平
#define LOOPMODE_9000_HIGHT_1                                1200 //9000模式1高电平

#define LOOPMODE_9000_LOW_0                                  200    //9000模式0低电平
#define LOOPMODE_9000_HIGHT_0                                800 //9000模式0高电平

#define LOOPMODE_9000_LOW_SPACE                              200    //9000模式空格位低电平
#define LOOPMODE_9000_HIGHT_SPACE                            1600 //9000模式空格位高电平


#define APP_LOOP_BACKCODE_SAMPLE_GAP                         20 //20us
#define APP_LOOP_BACKCODE_SAMPLE_DELAY_9000                  500 //500us
#define APP_LOOP_BACKCODE_SAMPLE_DELAY_EPC                   70 //70us
#define APP_LOOP_BACKCODE_SAMPLE_NUM                         10 //10us

#define LOOPID_NORMAL_CHECK                    0x01  //正常巡检
#define LOOPID_FAULT_CHECK                     0x06  //故障巡检
#define LOOPID_FIRE_CHECK                      0x08  //火警巡检
#define LOOPID_POINT_TEST                      0x10  //单点测试
#define LOOPID_WRITE_WORKMODE	               0X51  //写工作模式（灵敏）
#define LOOPID_READ_WORKMODE                   0x2B  //读工作模式
#define LOOPID_READ_ADREESS                    0x2F  //读码
#define LOOPID_WRITE_ADREESS                   0x55  //编码
#define LOOPID_WRITE_ALARMVALUE                0x62	 //写报警值
#define LOOPID_WRITE_ALARMVALUE_BYBE           0x6A	 //写报警值组合命令
#define LOOPID_READ_ALARMVALUE                 0x2C	 //读背景值和报警值
#define LOOPID_POINT_START                     0x14  //模块动作
#define LOOPID_POINT_STOP                      0x1F  //模块动作消除
#define LOOPID_MODEL_ACTION                    0x1D//动作确认
#define LOOPID_MODEL_STOP                      0x1C//动作停止
#define LOOPID_POINT_MASK                      0x13//屏蔽
#define LOOPID_ON_LAMP                         0x14//打开标志灯
#define LOOPID_OFF_LAMP                        0x1F//关闭标志灯


#define MKID_ELECTRICALDH_TEMPERATURE                    0x07   //测温式电气火灾探测器
#define MKID_ELECTRICALDH_CURRENT                        0x08   //剩余电流式探测器

typedef enum 
{
    
    LOOPMODE_9000=0,
    LOOPMODE_EPC,
    LOOPMODE_MAX,
}loopmode_e;

typedef struct 
{
    uint8_t cmd;
    uint16_t address;
    uint8_t cmdcheck;
    uint8_t addcheck;
    uint16_t backcodedate;
    uint8_t online;
    uint8_t backcodecheck;
}businfo_t;

typedef	struct 	
{										
    uint16_t halfbittime[78];
    uint8_t  item;
}loopsendcodeinfo_t;

typedef	struct 	
{										
    uint16_t backcodeadcvalbuff[20][16];
    uint8_t  backcodeadcvalbuffitem[20];//一个bit内的采样计数
    uint8_t  senditem;//当前bit计数
    uint16_t baseadcval;//基准电压
    uint8_t  samplenum;//采样数量
    uint8_t  samplegap;//采样间隔
    uint32_t sampledelay;//采样延时
}loopdecodedate_t;

typedef	struct 	
{										
   loopsendcodeinfo_t loopsendcodectrl;
   loopdecodedate_t loopdecodectrl;
   businfo_t sendinfo;
   loopmode_e mode;
}loopctrlinfo_t;






typedef struct 
{
    uint8_t cmd;
    uint16_t address;
    uint8_t cmdcheck;
    uint8_t addcheck;
    uint16_t backcodedate;
    uint8_t online;
    uint8_t backcodecheck;
}loopworkstatus_t;




void app_loop_init(void);
void app_loop_setmode(loopmode_e mode);
void app_loop_sendinfotobit(businfo_t *info, loopctrlinfo_t *ctrl);
uint8_t app_loop_open(void);
void app_loop_close(void);
void app_loop_sendbit_level(uint8_t level);
void app_loop_sendcodestart(loopctrlinfo_t *ctrl);
void app_loop_sendcodestop(void);
void app_loop_setdate(uint8_t flag, uint32_t time);
void app_loop_decodebuffsampleinit(loopctrlinfo_t *ctrl);
void app_loop_backcodesamplestart(loopctrlinfo_t *ctrl);
void app_loop_sendcode(loopctrlinfo_t *ctrl);
void app_loop_getbackcode(loopctrlinfo_t *ctrl);
uint32_t app_loop_decode(loopctrlinfo_t *ctrl);
int app_loop_writecode(loopmode_e mode, int adrr);
void app_loop_sendinfotobus(void);
uint8_t app_loop_pointtest(uint8_t mode, int adrr);
int app_loop_readcode(uint8_t mode,int *adrr);
uint8_t app_loop_modelmodeset(uint8_t mode,int modesel);
uint8_t app_loop_modelmodeget(uint8_t mode, int *modesel);
uint8_t app_loop_soundermodeset(uint8_t mode,int modesel);
uint8_t app_loop_soundermodeget(uint8_t mode, int *modesel);
uint8_t app_loop_operatelamp(int adrr,uint8_t onoff,uint8_t mode);
uint8_t app_loop_electricaldhset(uint8_t mode,uint16_t modesel);
uint8_t app_loop_electricaldhget(uint8_t mode, int *modesel);
uint8_t app_loop_operatelamp(int adrr,uint8_t onoff,uint8_t mode);
uint8_t app_loop_mulfunset(uint8_t mode,uint8_t subfun,uint16_t para,uint8_t blacklisten);
uint8_t app_loop_mulfunget(uint8_t mode, uint8_t subfun,uint16_t *modesel);
#ifdef __cplusplus
}
#endif

#endif /* __ZB32L03x_STARTERKIT_H */


